﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; 
using Wedo.Mvc.Pager;
using Wedo.Mvc.Utility;
using Wedo.Service.Common;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Domain.Models;
using Wedo.Domain.Models;
using Wedo.Recognition.Web.Controllers; 
using Wedo.Utility.Log;
using Wedo.Recognition.Repository;
using Wedo.Mvc.Utility.Models;
using Wedo.Utility.Common;  

namespace Wedo.MVC.Controllers
{
    public partial class UserInfoController : EntityController<UserInfo,SearchModel>
    {
        private UserService _uService = null;

        public UserInfoController(IUnitWork dbContext)
            : base(dbContext)
        { 
            _uService = new UserService(this._dbContext);
        }
 
        protected override void Insert(UserInfo model)
        {
            var Roles = GetRoleTypes();
            _uService.AddUser(model, Roles);
        }

        protected override UserInfo FindById(Guid Id)
        {
            var model = _uService.GetById(Id);

            return model;
        }

        protected override void UpdateModel(UserInfo model )
        {
            var roles = GetRoleTypes();
            if (roles.Count == 0)
                throw new Exception("请选择角色");


            var Roles = GetRoleTypes();
            model.Modified = DateTime.Now;
            model.ModifiedBy = CurrentUserName;

            _uService.UpdateUser(model, Roles);
        }

        protected List<string> GetRoleTypes()
        {
            if (Request.Form.GetValues("RoleTypes").Length > 0)
            {
                var Roles = Request.Form.GetValues("RoleTypes")
                    .ToList();
                return Roles;
            }
            return new List<string>();
        }

        public override ActionResult Delete(Guid id)
        {
            ResultMSG msg = new ResultMSG();
            try
            {
                _uService.Remove(id);
                msg.MSG = "删除用户成功";
                msg.Result = true;

            }
            catch (Exception ex)
            {
                msg.Result = false;
                msg.MSG = string.Format("删除用户失败,原因:{0}", ex.Message);
            }
            return Json(msg);
        }
 
        public ActionResult UserNavigators()
        {
            try
            {
                //var user = _uService.GetPageUser(CurrentUserName);
                var dict = _uService.GetNavigators(CurrentUserName);
                //IEnumerable<Navigation> dict = new List<Navigation>();
                //if (user != null)
                //{
                //    dict = Navigator.GetCurrentNav(user.Roles);
                //} 
                return PartialView("Navigator", dict);
            }
            catch (Exception ex)
            {
                Logger.LogDebug("User", "UserNavigators", ex.Message, CurrentUserName);

                return PartialView("Navigator", new List<CommonGroup<Module, Resource>>());
            }
        }

        protected override IBaseService<UserInfo> EntityService
        {
            get { return _uService; }
        }

        protected override PagedList<UserInfo> GetSearchPagedList(SearchModel where)
        {
            where=where??new SearchModel();
            var query=EntityService.GetAll();
            if(where.Keyword.IsNotNullOrWhiteSpace())
            {
                query=query.Where(m=>m.StaffNameEN.Contains(where.Keyword) ||m.StaffNameCN.Contains(where.Keyword) ||m.StaffCode.Contains(where.Keyword) );
            }
            return query.OrderBy(m => m.StaffCode).ToPagedList(where.Page, 10);
        }


        [HttpPost]
        public override ActionResult Edit(UserInfo model)
        {
            try
            {
                 //修改数据
                UpdateModel(model);
                return SuccessMsg("修改成功！");
            }
            catch (Exception ex)
            {
                return SuccessMsg(ex.Message);
            }
        }

        public override ActionResult Create()
        {
            ViewData["RoleTypes"]=GetRoleSelect(null);
            return base.Create();
        }

        public override ActionResult Edit(Guid id)
        {
            var model = FindById(id);
            ViewData["RoleTypes"] = GetRoleSelect(model);

            return ReturnAjaxView(model);
        }

        private List<SelectListItem> GetRoleSelect(UserInfo user)
        {
            var list = _dbContext.Role.All();
            List<SelectListItem> select = new List<SelectListItem>();
            if (user != null)
            {
                foreach (var item in list)
                {
                    select.Add(new SelectListItem { Value = item.Id, Text = item.RoleName, Selected = user.Roles.Any(m => m.Id == item.Id) });
                }
            }
            return select;
        }
    }
}
